//
//  AddAndLessView.m
//  AddAndLessDemo
//
//  Created by WillyZhao on 2017/6/12.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "AddAndLessView.h"


@interface AddAndLessView () <UITextFieldDelegate> {
    
    UIButton *pBtn;
    UIButton *btn_less;
    UITextField *textCount;
}

@end

@implementation AddAndLessView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubViews:frame];
    }
    return self;
}

-(void)initializeSubViews:(CGRect)frame {
    
    //底部view
    UIView *vc = [[UIView alloc] initWithFrame:CGRectMake(10, (frame.size.height - 30)/2.0, 111, 30)];
    vc.layer.borderColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1].CGColor;
    vc.layer.borderWidth = 1.0;
    vc.layer.cornerRadius = 5.0f;
    [self addSubview:vc];
    
    //添加第一个竖线
    UIView *vc1 = [[UIView alloc] initWithFrame:CGRectMake(31, 1, 1, 28)];
    vc1.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
    [vc addSubview:vc1];
    
    //添加第二个竖线
    UIView *vc2 = [[UIView alloc] initWithFrame:CGRectMake(vc.frame.size.width - 31, 1, 1, 28)];
    vc2.backgroundColor = vc1.backgroundColor;
    [vc addSubview:vc2];
    
    //添加按钮
    btn_less = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_less.frame = CGRectMake(1, 1, vc1.frame.origin.x - 1, 28);
    [btn_less setTitle:@"-" forState:UIControlStateNormal];
    [btn_less setTitleColor:vc1.backgroundColor forState:UIControlStateNormal];
    btn_less.titleLabel.font = [UIFont systemFontOfSize:18];
    btn_less.tag = 1;
    [vc addSubview:btn_less];
    
    //添加按钮
    UIButton *btn_add = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_add.frame = CGRectMake(vc2.frame.origin.x+1, 1, 30, 28);
    [btn_add setTitle:@"+" forState:UIControlStateNormal];
    [btn_add setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn_add.titleLabel.font = [UIFont systemFontOfSize:18];
    btn_add.tag = 2;
    [vc addSubview:btn_add];
    [btn_add addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn_less addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //添加label
    textCount = [[UITextField alloc] initWithFrame:CGRectMake(vc1.frame.origin.x+1, 1, vc2.frame.origin.x - vc1.frame.origin.x - 1, 28)];
    textCount.textAlignment = NSTextAlignmentCenter;
    textCount.text = @"1";
    textCount.delegate = self;
    textCount.font = [UIFont systemFontOfSize:15];
    textCount.textColor = [UIColor blackColor];
    textCount.userInteractionEnabled = NO;
    [vc addSubview:textCount];
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text integerValue]>1) {
        //改变减号颜色为可点
        [btn_less setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }else{
        [btn_less setTitleColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1] forState:UIControlStateNormal];
    }
    
    //返回数据
    if (self.clickedString) {
        self.clickedString(textCount.text);
    }
}

#pragma mark 按钮点击方法
-(void)buttonAction:(UIButton *)btn {
    NSInteger index = [textCount.text integerValue];
    if (btn.tag == 1) {
        //减
        if (index>=2) {
            textCount.text = [NSString stringWithFormat:@"%ld",--index];
        }else{
            textCount.text = @"1";
        }
    }else{
        //加
        textCount.text = [NSString stringWithFormat:@"%ld",++index];
        [btn_less setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    if ([textCount.text integerValue] <= 1) {
        [btn_less setTitleColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1] forState:UIControlStateNormal];
    }
    
    //返回数据
    if (self.clickedString) {
        self.clickedString(textCount.text);
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
